@extends('layouts.app')

@section('content')
    @include('vendor.ueditor.assets')
    <div class="container">
        <div class="row">
            <div class="col-md-8 col-md-offset-1">
                <div class="panel panel-default">
                    {{-- 编辑器容器 --}}
                    <div class="panel-heading">{{ $question->title }}
                        @foreach($question->topics as $topic)
                            <a class="topic pull-right" href="/topic/{{ $topic->id }}">{{ $topic->name }}</a>
                        @endforeach</div>
                    <div class="panel-body">
                        {!! $question->body !!}
                    </div>
                    <div class="actions">
                        @if (Auth::check() && Auth::user()->owns($question))
                            <span class="edit"><a
                                        href="{{route('question.edit', ['id' => $question->id])}}">编辑</a></span>
                            <form action="/question/{{$question->id}}" method="POST" class="delete-form">
                                {{ method_field('DELETE') }}
                                {{ csrf_field() }}
                                <button class="button is-naked delete-button">删除</button>

                            </form>
                        @endif

                    </div>
                </div>
            </div>
            <div class="col-md-3">
                <div class="panel panel-default">
                    <div class="panel-heading question-follow">
                        <h2>{{ $question->followers_count }}</h2>
                        <span>关注者</span>
                    </div>
                    <div class="panel-body">
                        <button-question-follow question="{{$question->id}}"></button-question-follow>
                        <a href="#editorContainer" class="btn btn-primary pull-right">撰写答案</a>
                    </div>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-md-8 col-md-offset-1">
                <div class="panel panel-default">
                    {{-- 编辑器容器 --}}
                    <div class="panel-heading">
                        {{ $question->answers_count }} 个答案
                    </div>

                    <div class="panel-body">
                        {{--显示所有回答--}}
                        @foreach($question->answers as $answer)
                            <div class="media">
                                <div class="media-left">

                                </div>
                                <div class="media-body">
                                    <h4 class="media-heading">
                                        <a href="/user/{{ $answer->user->name }}">
                                            {{ $answer->user->name }}
                                        </a>
                                    </h4>
                                    {!! $answer->body !!}
                                </div>
                                <comments type="answer"
                                          model="{{$answer->id}}"
                                          count="{{$answer->comments()->count()}}">
                                </comments>
                            </div>
                        @endforeach
                        {{--回复框--}}
                        @if(Auth::check())
                            <form action="/question/{{$question->id}}/answer" method="post">
                                {!! csrf_field() !!}
                                <div class="form-group{{ $errors->has('body') ? ' has-error' : '' }}">
                                    <label for="body">描述</label>
                                    <script id="editorContainer" name="body" type="text/plain">
                                        {!! old('body')  !!}
                                    </script>
                                    @if ($errors->has('body'))
                                        <span class="help-block">
                                            <strong>{{ $errors->first('body') }}</strong>
                                        </span>
                                    @endif
                                </div>
                                <button class="btn btn-default pull-right" type="submit">回复</button>
                            </form>
                        @else
                            <a href="{{ url('login') }}" class="btn btn-success btn-block">登录提交答案</a>
                        @endif
                    </div>
                </div>
            </div>
            <div class="col-md-3">
                <div class="panel panel-default">
                    <div class="panel-heading question-follow">
                        <h5>关于作者</h5>
                    </div>
                    <div class="panel-body">
                        <div class="media">
                            <div class="media-left">
                                <a href="#"><img src="{{ $question->user->avatar }}" alt="" width="40"></a>
                            </div>
                            <div class="media-body">
                                <h4 class="media-heading"><a href="#">{{ $question->user->name }}</a></h4>
                            </div>
                            <div class="user-statics">
                                <div class="statics-item text-center">
                                    <div class="statics-text">问题</div>
                                    <div class="statics-count">{{$question->user->questions_count}}</div>
                                </div>
                                <div class="statics-item text-center">
                                    <div class="statics-text">回答</div>
                                    <div class="statics-count">{{$question->user->answers_count}}</div>
                                </div>
                                <div class="statics-item text-center">
                                    <div class="statics-text">关注者</div>
                                    <div class="statics-count">{{$question->user->followers_count}}</div>
                                </div>
                            </div>
                        </div>
                        @if(Auth::check())
                        <button-user-follow user="{{$question->user_id}}"></button-user-follow>
                        <send-message user="{{$question->user_id}}"></send-message>
                        @endif
                    </div>
                </div>
            </div>
        </div>
    @section('js')
            {{-- 实例化编辑器 --}}
            @if(Auth::check())
            <script type="text/javascript">
                var ue = UE.getEditor('editorContainer', {
                    toolbars: [
                        ['bold', 'italic', 'underline', 'strikethrough', 'blockquote', 'insertunorderedlist', 'insertorderedlist', 'justifyleft', 'justifycenter', 'justifyright', 'link', 'insertimage', 'fullscreen']
                    ],
                    elementPathEnabled: false,
                    enableContextMenu: false,
                    autoClearEmptyNode: true,
                    wordCount: false,
                    imagePopup: false,
                    autotypeset: {indent: true, imageBlockLine: 'center'}
                });
                ue.ready(function () {
                    ue.execCommand('serverparam', '_token', '{{ csrf_token() }}'); // 设置 CSRF token.
                });

            </script>
            @endif
        @endsection
    </div>
@endsection


